home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-02-02 | 9.1 KB | 329 lines | [TEXT/PJMM] |
- { This Unit contains the objects to implement look-alikes of }
- { the standard Macintosh controls: Button, Checkbox, and Radio button }
- { The standard controls are much more robust. These are used }
- { as an example for Object Programming within drivers }
-
- unit ControlObjUnit;
-
- interface
-
- type
- { tControlObj is our base class object for all controls }
- tControlObj = object
- fControl: ControlHandle; { the control we are using }
- procedure mInit (theControl: ControlHandle); { InitCntl }
- procedure mFree; { DispCntl }
- procedure mDraw (param: LongInt); { DrawCntl }
- function mTest (param: LongInt): LongInt; { TestCntl }
- procedure mCalcRgn (param: LongInt; message: integer); { CalcCRgns, calcCntlRgn }
- procedure mPosition (param: LongInt); { PosCntl }
- procedure mCalcThumb (param: LongInt); { ThumbCntl }
- function mDrag (param: LongInt): LongInt; { DragCntl }
- procedure mAutoTrack (param: LongInt); { autoTrack }
- end; { object tControlObj }
-
-
- { our class to handle the basic button codes }
- tButtonMainObj = object(tControlObj)
- fOldFont, fOldSize: integer;
- function mTest (param: LongInt): LongInt;
- override;
- procedure mCalcRgn (param: LongInt; message: integer); { CalcCRgns, calcCntlRgn }
- override;
- function mGetPartCode: integer;
- procedure mDrawTitle (hOffset: integer);
- procedure SetFont;
- procedure RestoreFont;
- end; { object tButtonMainObj }
-
- { button routine }
- tPushButObj = object(tButtonMainObj)
- procedure mDraw (param: LongInt);
- override;
- function mGetPartCode: integer;
- override;
- end; { object tPushButObj }
-
- { checkBox routine }
- tCheckBoxObj = object(tButtonMainObj)
- procedure mDraw (param: LongInt);
- override;
- procedure mDrawCheckFrame (frame: Rect);
- end; { object tCheckBoxObj }
-
- { radio button routine }
- tRadioButObj = object(tCheckBoxObj)
- procedure mDrawCheckFrame (frame: Rect);
- override;
- end; { object tRadioButObj }
-
- implementation
-
- {------------------------------}
- procedure tControlObj.mInit (theControl: ControlHandle); { InitCntl }
- begin
- fControl := theControl;
- end; { tControlObj.mInit }
-
- {------------------------------}
- procedure tControlObj.mFree; { DispCntl }
- begin
- Dispose(SELF);
- end; { tControlObj.mFree }
-
- {------------------------------}
- procedure tControlObj.mDraw; { DrawCntl }
- begin
- end; { tControlObj.mDraw }
-
- {------------------------------}
- function tControlObj.mTest (param: LongInt): LongInt;
- { TestCntl }
- begin
- end; { tControlObj.mTest }
-
- {------------------------------}
- procedure tControlObj.mCalcRgn (param: LongInt; message: integer);
- { CalcCRgns, calcCntlRgn }
- begin
- end; { tControlObj.mCalcRgn }
-
- {------------------------------}
- procedure tControlObj.mPosition (param: LongInt);
- { PosCntl }
- begin
- end; { tControlObj.mPosition }
-
- {------------------------------}
- procedure tControlObj.mCalcThumb (param: LongInt);
- { ThumbCntl }
- begin
- end; { tControlObj.mCalcThumb }
-
- {------------------------------}
- function tControlObj.mDrag (param: LongInt): LongInt;
- { DragCntl }
- begin
- end; { tControlObj.mDrag }
-
- {------------------------------}
- procedure tControlObj.mAutoTrack (param: LongInt);
- { autoTrack }
- begin
- end; { tControlObj.mAutoTrack }
-
- {------------------------------}
- function tButtonMainObj.mTest (param: LongInt): LongInt;
- { Valid as long as its within the contrlRect and the }
- { button isn't dimmed }
- var
- thePoint: Point;
- begin
- mTest := 0;
- thePoint := Point(param);
- if fControl^^.contrlHilite < 254 then
- if PtInRect(thePoint, fControl^^.contrlRect) then
- mTest := mGetPartCode;
-
- end; { tButtonMainObj.mTest }
-
- {------------------------------}
- procedure tButtonMainObj.mCalcRgn (param: LongInt; message: integer);
- { These controls use their full rectangle as the region }
- var
- TheRgnHdl: Rgnhandle;
- begin
- theRgnHdl := RgnHandle(Param);
- if message = CalcCRgns then
- BitClr(Ptr(theRgnHdl^), 0); { clear the high bit for 32 bit compatibility }
- RectRgn(theRgnHdl, fControl^^.contrlRect);
- end; { tButtonMainObj.mCalcRgn }
-
- {------------------------------}
- function tButtonMainObj.mGetPartCode: integer;
- { both checkbox and radio button return inCheckBox }
- { We will override for regular button }
- begin
- mGetPartCode := inCheckBox;
- end; { tButtonMainObj.mGetPartCode }
-
- {------------------------------}
- procedure tButtonMainObj.SetFont;
- var
- curPort: GrafPtr;
- begin
- GetPort(curPort);
- fOldFont := curPort^.txFont;
- fOldSize := curPort^.txSize;
- TextFont(0);
- TextSize(12);
- end; { tButtonMainObj.SetFont}
-
- {------------------------------}
- procedure tButtonMainObj.RestoreFont;
- begin
- TextFont(fOldFont);
- TextSize(fOldSize);
- end; { tButtonMainObj.RestoreFont}
-
- {------------------------------}
- procedure tButtonMainObj.mDrawTitle (hOffset: integer);
- { draws and optionally dims the title of the control }
- { parameters are the offset from the rectangle top, left border }
- { Assumes the caller has erased the area }
- var
- dispRect: Rect;
- theRgn: RgnHandle;
- theFontInfo: FontInfo;
- rectHeight, textHeight, vOffset: integer;
- GrayPat: Pattern;
- begin
- SetFont;
- dispRect := fControl^^.contrlRect;
- TheRgn := NewRgn; { set up a clip }
- GetClip(TheRgn);
- ClipRect(dispRect);
-
- GetFontInfo(theFontInfo);
- rectHeight := (dispRect.bottom - dispRect.top);
- textHeight := theFontInfo.ascent + theFontInfo.leading + theFontInfo.descent;
- vOffset := (rectHeight - textHeight) div 2;
-
- dispRect.left := dispRect.left + hOffset;
- MoveTo(dispRect.Left, dispRect.Top + vOffset + theFontInfo.ascent + theFontInfo.leading);
- DrawString(fControl^^.contrlTitle);
-
- if fControl^^.ContrlHilite = 255 then
- begin { dim the text }
- GetIndPattern(GrayPat, 0, 4); { gray }
- PenPat(GrayPat);
- PenMode(PatBic);
- PaintRect(dispRect);
- PenNormal;
- end;
-
- SetClip(TheRgn);
- DisposeRgn(theRgn);
- RestoreFont;
- end; { tButtonMainObj.mDrawTitle }
-
- {------------------------------}
-
- procedure tPushButObj.mDraw (param: LongInt);
- { draws the standard button }
- var
- dispRect: Rect;
- hOffset: integer;
- begin
- if (fControl^^.contrlVis <> 0) & (param <> 128) then
- begin
- dispRect := fControl^^.contrlRect;
- hOffset := (dispRect.right - dispRect.left - StringWidth(fControl^^.contrlTitle)) div 2;
-
- { now draw everything }
- EraseRect(dispRect);
-
- mDrawTitle(hOffset);
-
- FrameRoundRect(dispRect, 16, 16); { draw frame }
- if fControl^^.contrlHilite = InButton then
- begin { invert it }
- InsetRect(dispRect, 1, 1);
- InvertRoundRect(dispRect, 16, 16);
- end;
-
- end; { if fControl^^.contrlVis }
-
- end; { tPushButObj.mDraw }
-
- {------------------------------}
- function tPushButObj.mGetPartCode: integer;
- begin
- mGetPartCode := inButton;
- end; { tPushButObj.mGetPartCode }
-
- {------------------------------}
- procedure tCheckBoxObj.mDraw (param: LongInt);
- { handles both the radio button and checkbox because }
- { they are basically the same }
- const
- cBoxSize = 12; { size of the checkbox }
- cTextStart = 17; { Text starts after the box }
- var
- dispRect, boxRect: Rect;
- Gray: Pattern;
- TheFInfo: FontInfo;
- vOffset, vBoxOffset, rectHeight, textHeight: integer;
- begin
- if (fControl^^.contrlVis <> 0) & (param <> 128) then
- begin
- dispRect := fControl^^.contrlRect;
-
- vBoxOffset := (dispRect.bottom - dispRect.top - cBoxSize) div 2;
-
- { Draw the checkbox without changing the existing text }
- BoxRect := dispRect;
- BoxRect.top := BoxRect.top + vBoxOffset;
- BoxRect.Bottom := BoxRect.top + cBoxSize;
- BoxRect.Right := BoxRect.left + cBoxSize;
- EraseRect(BoxRect);
- mDrawCheckFrame(BoxRect);
-
- { Draw the text }
- if (fControl^^.contrlHilite <> inCheckBox) & (fControl^^.contrlTitle <> '') & (Param = 0) then
- begin { we have text }
-
- dispRect.left := dispRect.left + cTextStart;
- EraseRect(dispRect); { erase old text }
- mDrawTitle(cTextStart);
- end; { if (Title <> '') }
- end; { if fControl^^.contrlVis }
-
- end; { tCheckBoxObj.mDraw }
-
- {------------------------------}
- procedure tCheckBoxObj.mDrawCheckFrame (frame: Rect);
- { draws the checkbox image and highlights it if needed }
- begin
- FrameRect(frame);
-
- if fControl^^.contrlValue = 1 then { draw the checkbox }
- begin
- MoveTo(frame.left, frame.Top);
- LineTo(frame.right - 1, frame.bottom - 1);
- MoveTo(frame.right - 1, frame.top);
- LineTo(frame.left, frame.bottom - 1);
- end;
-
- if fControl^^.contrlHilite = inCheckBox then { when drawing hiliting, we don't need to change the text }
- begin
- InsetRect(frame, 1, 1);
- FrameRect(frame);
- end;
-
- end; { tCheckBoxObj.mDrawFrame }
-
- {------------------------------}
- procedure tRadioButObj.mDrawCheckFrame (frame: Rect);
- { draws the radio button image and highlights it if needed }
- begin
- FrameOval(frame);
-
- if fControl^^.contrlValue = 1 then { radio hilite }
- begin
- InsetRect(frame, 3, 3);
- PaintOval(frame); { draw the indicator }
- InsetRect(frame, -3, -3);
- end;
-
- if fControl^^.contrlHilite = inCheckBox then { when drawing hiliting, we don't need to change the text }
- begin
- InsetRect(frame, 1, 1);
- FrameOval(frame);
- end;
-
- end; { tRadioButObj.mDrawFrame }
-
- {------------------------------}
-
- end. { unit ControlObjUnit }